package com.powergisol.gis.gzip;

import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class MyResponse extends HttpServletResponseWrapper {
    private HttpServletResponse response;
    private ByteArrayOutputStream bout=new ByteArrayOutputStream();

    public MyResponse(HttpServletResponse response) {
        super(response);
        this.response=response;
    }

    /*综合考虑下还是重写getOutStream
    但是需求是要输出到缓存，此方法不适合，于是写一个拓展类类来迎合需求*/
    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        return new MyServletOutputStream(bout);
    }


    //将数据封装成一个数组然后发送到缓存
    class MyServletOutputStream extends ServletOutputStream{
        private ByteArrayOutputStream bout;
        public MyServletOutputStream(ByteArrayOutputStream bout){
            this.bout=bout;

        }

        /*此处应该要将数据包装成一个数组但是
            此处不符合需求，于是重载*/
        @Override
        public void write(int b) throws IOException {

        }
        public void write(byte[] bytes) throws IOException {
            bout.write(bytes);
            bout.flush();
            bout.close();
        }
        @Override
        public boolean isReady() {
            return false;
        }

        @Override
        public void setWriteListener(WriteListener listener) {

        }
    }
    public byte[] getBuffer() {
        return bout.toByteArray();
    }
}
